﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Zmotion406.Service;

namespace Zmotion406
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            GetIps();
            toolTip1.SetToolTip(InitConnect, "点击此处将默认序列化本地已储存的各轴配置信息，如未储存过，请忽略本条提示！");

        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            UserControl configForm = new Views.ConfigForm();
            SwitchUserControl(configForm);
        }
        private void SwitchUserControl(UserControl userControl)
        {
            if (userControl == null) return;
            this.MainPanel.Controls.Clear();
            userControl.Dock = DockStyle.Fill;
            this.MainPanel.Controls.Add(userControl);
        }

        

        private void button5_Click(object sender, EventArgs e)
        {
            UserControl exerciseForm = new Views.ExerciseForm();
            SwitchUserControl(exerciseForm);
        }
        /// <summary>
        /// 获取串口IP
        /// </summary>
        private void GetIps()
        {
            List<string> list = ConfigService.GetSerialPort();
            comboBox_ips.DataSource = list;
        }

        private void Search_ips_Click(object sender, EventArgs e)
        {
            GetIps();
        }

        private void connectHandle_Click(object sender, EventArgs e)
        {
            string label = connectHandle.Text;
            if (string.Equals(label, "连接"))
            {
                string? ip = comboBox_ips.SelectedValue?.ToString() ?? string.Empty;
                if (string.IsNullOrEmpty(ip))
                {
                    MessageBox.Show("请选择有效IP地址");
                    return;
                }
                int res = ConfigService.ConnectEquipment(ip);
                if (res == 1)
                {
                    connectHandle.Text = "断开";
                    connectHandle.BackColor = Color.Green;
                }
            }
            else if (string.Equals(label, "断开"))
            {
                int res = ConfigService.CloseConnect();
                if (res == 1)
                {
                    connectHandle.Text = "连接";
                    connectHandle.BackColor = Color.Tomato;
                }
                else
                {
                    MessageBox.Show("断开连接失败！");
                }
            }
        }

        private void InitConnect_Click(object sender, EventArgs e)
        {
            ConfigService.GetJsonAxisConfig();
        }
    }
}
